<template>
  <div>
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <!-- <el-form-item label="分类名称" prop="class">
        <el-select
          clearable
          v-model="form.class"
          placeholder="请选择分类名称"
          style="width: 100%"
        >
          <el-option
            v-for="(item, index) in classList"
            :key="index"
            :value="item.value"
            >{{ item.label }}</el-option
          >
        </el-select>
      </el-form-item> -->
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入模板名称"
          maxlength="50"
        />
      </el-form-item>
      <el-form-item label="图片" prop="imgurl">
        <el-input
          v-model="form.imgurl"
          readonly=""
          placeholder="请上传图片"
          class="imginput"
        >
          <template slot="append">
            <div class="commhover full_w inputbox">
              上传图片
              <input
                type="file"
                name=""
                class="uploadinput"
                id="uploadimg"
                @change="changeImg"
              />
            </div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="" v-if="form.imgurl">
        <img src="@/assets/images/hbg1.png" alt="" class="imgurl" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input type="number"
          v-model="form.sort"
          placeholder="请输入排序"
          maxlength="50"
        />
      </el-form-item>
<!--       
      <el-form-item label="模板分类" prop="template">
        <el-select
          clearable
          v-model="form.template"
          placeholder="请选择模板分类"
          style="width: 100%"
        >
          <el-option
            v-for="(item, index) in tempList"
            :key="index"
            :value="item.value"
            >{{ item.label }}</el-option
          >
        </el-select>
      </el-form-item>
      <el-form-item label="默认设置代码" prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入默认设置代码"
          type="textarea"
          rows="3"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item> -->
    </el-form>
    <div class="flexend">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="handleCloseAdd()">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { isOverSize } from "@/utils/validate";
import { uploadPhoto } from "@/api/comm";
export default {
  data() {
    return {
      form: {},
      classList: [{ label: "抽奖专用模板", value: 1 }],
      tempList: [{ label: "抽奖活动", value: 1 }],
      // 表单校验
      rules: {
        class: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        imgurl: [
          { required: true, message: "预览图不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "模板名称不能为空", trigger: "blur" },
        ],
        template: [
          { required: true, message: "模板分类不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    reset() {
      this.form = {
        id: undefined,
        class: undefined,
        name: undefined,
        imgurl: undefined,
        template: undefined,
        status: 1,
        menuIds: [],
      };
      this.resetForm("form");
    },

    handleCloseAdd(type) {
      this.$emit("close", type);
    },

    // 上传图片
    changeImg(event) {
      var file = event.currentTarget.files[0];
      console.log("file:", file, event);
      if (isOverSize(file.size, 5)) {
        this.$message.warning("图片大小不能超过5MB");
        return;
      }
      var formData = new FormData();
      formData.append("photo", file);
      uploadPhoto(formData).then((res) => {
        if (res.code == 1) {
          // console.log('图片：',res.data);
          this.$message.success("上传成功");
          let url = res.data.replace("\\", "/");
          this.form.imgurl = process.env.VUE_APP_BASE_API + "/" + url;
          document.getElementById("uploadimg").value = null;
        }
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.roleId != undefined) {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            updateRole(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            addRole(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.inputbox {
  position: relative;
  padding: 0 20px;
  cursor: pointer;
}
.uploadinput {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  cursor: pointer;
}
.imginput {
  ::v-deep .el-input-group__append {
    padding: 0 !important;
  }
}
.imgurl {
  width: 200px;
  height: 80px;
}
</style>